local wezterm = require("wezterm")
local platform = require("utils.platform")()
local act = wezterm.action

local keys = {
	-- shortcut for ToggleFullScreen
	{ key = "F11",   mods = "NONE",         action = act.ToggleFullScreen },
	{ key = "Enter", mods = "ALT",          action = act.ToggleFullScreen },
	-- Send "CTRL-B" to the terminal when pressing CTRL-B, CTRL-A
	{ key = "a",     mods = "LEADER|CTRL",  action = act({ SendString = "\x01" }) },
	{ key = "-",     mods = "LEADER",       action = act({ SplitVertical = { domain = "CurrentPaneDomain" } }) },
	-- leader \
	{ key = "\\",    mods = "LEADER",       action = act({ SplitHorizontal = { domain = "CurrentPaneDomain" } }) },
	{ key = "z",     mods = "LEADER",       action = "TogglePaneZoomState" },
	{ key = "c",     mods = "LEADER",       action = act({ SpawnTab = "CurrentPaneDomain" }) },
	{ key = "h",     mods = "LEADER",       action = act({ ActivatePaneDirection = "Left" }) },
	{ key = "j",     mods = "LEADER",       action = act({ ActivatePaneDirection = "Down" }) },
	{ key = "k",     mods = "LEADER",       action = act({ ActivatePaneDirection = "Up" }) },
	{ key = "l",     mods = "LEADER",       action = act({ ActivatePaneDirection = "Right" }) },
	{ key = "H",     mods = "LEADER|SHIFT", action = act({ AdjustPaneSize = { "Left", 5 } }) },
	{ key = "J",     mods = "LEADER|SHIFT", action = act({ AdjustPaneSize = { "Down", 5 } }) },
	{ key = "K",     mods = "LEADER|SHIFT", action = act({ AdjustPaneSize = { "Up", 5 } }) },
	{ key = "L",     mods = "LEADER|SHIFT", action = act({ AdjustPaneSize = { "Right", 5 } }) },
	{ key = "1",     mods = "LEADER",       action = act({ ActivateTab = 0 }) },
	{ key = "2",     mods = "LEADER",       action = act({ ActivateTab = 1 }) },
	{ key = "3",     mods = "LEADER",       action = act({ ActivateTab = 2 }) },
	{ key = "4",     mods = "LEADER",       action = act({ ActivateTab = 3 }) },
	{ key = "5",     mods = "LEADER",       action = act({ ActivateTab = 4 }) },
	{ key = "6",     mods = "LEADER",       action = act({ ActivateTab = 5 }) },
	{ key = "7",     mods = "LEADER",       action = act({ ActivateTab = 6 }) },
	{ key = "8",     mods = "LEADER",       action = act({ ActivateTab = 7 }) },
	{ key = "9",     mods = "LEADER",       action = act({ ActivateTab = 8 }) },
	{ key = "&",     mods = "LEADER|SHIFT", action = act({ CloseCurrentTab = { confirm = true } }) },
	{ key = "x",     mods = "LEADER",       action = act({ CloseCurrentPane = { confirm = true } }) },
	{ key = "q",     mods = "LEADER",       action = act.QuitApplication },

	-- navigate tabs, panels
	{ key = "7",     mods = "CTRL",         action = act.PaneSelect },
	{ key = "8",     mods = "CTRL",         action = act.TogglePaneZoomState },
	{ key = "9",     mods = "CTRL",         action = act.ActivateTabRelative(-1) },
	{ key = "0",     mods = "CTRL",         action = act.ActivateTabRelative(1) },
	{ key = "h",     mods = "CTRL|SHIFT",   action = act.ActivatePaneDirection("Left") },
	{ key = "l",     mods = "CTRL|SHIFT",   action = act.ActivatePaneDirection("Right") },
	{ key = "j",     mods = "CTRL|SHIFT",   action = act.ActivatePaneDirection("Down") },
	{ key = "k",     mods = "CTRL|SHIFT",   action = act.ActivatePaneDirection("Up") },

	{ key = "n",     mods = "SHIFT|CTRL",   action = "ToggleFullScreen" },
	{ key = "v",     mods = "CTRL",         action = act.PasteFrom("Clipboard") },
	{ key = "c",     mods = "CTRL",         action = act.CopyTo("Clipboard") },
	{ key = "+",     mods = "SHIFT|CTRL",   action = "IncreaseFontSize" },
	{ key = "-",     mods = "SHIFT|CTRL",   action = "DecreaseFontSize" },
	{ key = "0",     mods = "SHIFT|CTRL",   action = "ResetFontSize" },
	{ key = "l",     mods = "ALT",          action = act.ShowLauncher },
	{ key = "P",     mods = "SHIFT|CTRL",   action = act.ActivateCommandPalette },
}

local mouse_bindings = {
	-- Ctrl-click will open the link under the mouse cursor
	{
		event = { Up = { streak = 1, button = "Left" } },
		mods = "CTRL",
		action = act.OpenLinkAtMouseCursor,
	},
	-- Move mouse will only select text and not copy text to clipboard
	{
		event = { Down = { streak = 1, button = "Left" } },
		mods = "NONE",
		action = act.SelectTextAtMouseCursor("Cell"),
	},
	{
		event = { Up = { streak = 1, button = "Left" } },
		mods = "NONE",
		action = act.ExtendSelectionToMouseCursor("Cell"),
	},
	{
		event = { Drag = { streak = 1, button = "Left" } },
		mods = "NONE",
		action = act.ExtendSelectionToMouseCursor("Cell"),
	},
	-- Triple Left click will select a line
	{
		event = { Down = { streak = 3, button = "Left" } },
		mods = "NONE",
		action = act.SelectTextAtMouseCursor("Line"),
	},
	{
		event = { Up = { streak = 3, button = "Left" } },
		mods = "NONE",
		action = act.SelectTextAtMouseCursor("Line"),
	},
	-- Double Left click will select a word
	{
		event = { Down = { streak = 2, button = "Left" } },
		mods = "NONE",
		action = act.SelectTextAtMouseCursor("Word"),
	},
	{
		event = { Up = { streak = 2, button = "Left" } },
		mods = "NONE",
		action = act.SelectTextAtMouseCursor("Word"),
	},
	-- Turn on the mouse wheel to scroll the screen
	{
		event = { Down = { streak = 1, button = { WheelUp = 1 } } },
		mods = "NONE",
		action = act.ScrollByCurrentEventWheelDelta,
	},
	{
		event = { Down = { streak = 1, button = { WheelDown = 1 } } },
		mods = "NONE",
		action = act.ScrollByCurrentEventWheelDelta,
	},
}

return {
	disable_default_key_bindings = true,
	disable_default_mouse_bindings = true,
	leader = { key = "b", mods = "CTRL", timeout_milliseconds = 5000 },
	keys = keys,
	mouse_bindings = mouse_bindings,
}
